qui(
    function (require, exports, module) {

        Record.Channel_Signal_Dao = $.extend({}, EpgWeb.Dao, {
            addRecordChannelSignal:function(sendData,handler){
                this.sendAjax(Record.urlConfig.doChannelSignalSaveUrl,"post",sendData,handler,"/record/channel/signal/save");
            },
            deleteRecordChannelSignal:function(sendData,handler){
                this.sendAjax(Record.urlConfig.doChannelSignalDelUrl,"post",sendData,handler,"/record/channel/signal/del");
            },
            editRecordChannelSignal:function(sendData,handler){
                this.sendAjax(Record.urlConfig.doChannelSignalEditUrl,"post",sendData,handler,"/record/channel/signal/edit");
            },
            getRecordChannelSignal:function(entityId,handler){
                this.sendAjax(Record.urlConfig.doChannelSignalGetByIdUrl,"post",{id:entityId},handler,"/record/channel/signal/getById");
            },
            getRecordChannelSignalList :function(sendData,handler){
                this.sendAjax(Record.urlConfig.getChannelSignalLibraryListUrl ,"post",sendData,handler,"/record/channel/signal/libraryList");
            },
            startRecordChannelSignal :function(sendData,handler){
                this.sendAjax(Record.urlConfig.doChannelSignalStartUrl ,"post",sendData,handler,"/record/channel/signal/start");
            },
            stopRecordChannelSignal :function(sendData,handler){
                this.sendAjax(Record.urlConfig.doChannelSignalStopUrl ,"post",sendData,handler,"/record/channel/signal/stop");
            },

        });

        Record.Channel_Signal_Grid = function (options) {
            this.dao = Record.Channel_Signal_Dao;
            this.config = Record.Signal_Config;
            this.util = Record.Signal_Util;
            this.options = options || {};
        };

        Com.inherits(Record.Channel_Signal_Grid, Ancestor.Grid).extend({
            doInit: function () {
                var self = this;
                EpgWeb.ajaxQueue.afterLoadComplete(function () {
                    self.initChannel();
                    self.bindEvent();
                    self.initGrid();
                    self.initDlg();
                    self.initSuggest();
                })
                EpgWeb.ajaxQueue.startLoad();
            },
            initChannel:function () {
                //get channelId -> name code
                var channelId = Ancestor.Util.getUrlId("channel_id");
                if(channelId > 0) {
                    Record.Dao.getRecordChannelList({paramsObj: JSON.stringify({EQL_id: channelId})}, function (data) {
                        if (data != null && data.code == "A00000") {
                            var result = data.data.result[0];
                            $("#title_channel_id").text(result.id);
                            $("#title_channel_name").text(result.channelName);
                            $("#title_channel_code").text(result.channelCode);

                            $("#label_channel_id").text(result.id);
                            $("#label_channel_name").text(result.channelName);
                            $("#label_channel_code").text(result.channelCode);


                        } else {
                            Record.Util.logTips({theme: "error", content: "访问频道数据失败"});
                        }
                    });
                }else {
                    Record.Util.logTips({theme: "error", content: "channel_id 不正确！"});

                }


            },

            bindEvent: function () {

                //创建请求
                var $sendAddButton = $("button[name=add_channel_signal_btn]");
                $sendAddButton.bind("click", function (event) {
                    Record.Signal_Util.sendAddRecordChannelSignal();
                });

                //修改请求
                /*var $sendEditButton = $("button[name=edit_channel_signal_btn]");
                $sendEditButton.bind("click", function (event) {
                    Record.Signal_Util.sendEditRecordChannelSignal();
                });*/

                //新建
                $("button[name=open_new_dlg_btn]").bind("click", function (event) {

                    $("form[role=addForm]").dialog("open");
                    $("form[role=addForm]")[0].reset();

                });

                //取消
                $("button[name=cancel_btn]").bind("click", function (event) {
                    $("form[role=addForm]").dialog("close")
                });
            },

            initGrid: function (options) {
                var self = this;
                self.$grid = self.options.$grid;
                var params = self.getParam();
                var gridConfig = self.getGridConfig(self.config.CHANNEL_SIGNAL_DATA_LIST);
                options = $.extend(true, {
                    mtype: 'post',
                    postData: $.extend(true, {}, params)
                }, gridConfig, options);
                self.$grid.jqGrid(options);

                //简单响应式
                // $(window).resize(function(){
                //     self.$grid.setGridWidth($(window).width()*0.9);　
                // });

            },
            query: function (callback) {
                var self = this;
                var $grid = self.$grid;
                var params = self.getParam();
                params.sortType = "desc";
                $grid.jqGrid('setGridParam', {
                    page: 1,
                    datatype: "json",
                    postData: params
                }).trigger('reloadGrid');
            },
            getParam: function () {
                var channelId = Ancestor.Util.getUrlId("channel_id");
                var params = {paramsObj: JSON.stringify({'EQL_recordChannelSignal.channel_id': channelId})};
                console.log("query params:" + JSON.stringify(params));
                return params;
            },
            initDlg: function () {
                $("form[role=addForm]").dialog({
                    title: "关联信号",
                    autoOpen: false,
                    height: 640,
                    width: 840,
                    modal: true
                })
            },
            initSuggest: function () {
                var signalSuggest = {
                    options: {
                        ajaxSettings: {
                            beforeSend: function (request) {
                                var authBase = Ancestor.Util.getAuth("/record/signal/libraryList");
                                request.setRequestHeader('Sign', authBase.sign);
                                request.setRequestHeader("Time", authBase.clientTime);
                                request.setRequestHeader("From", authBase.from);
                                request.setRequestHeader("UserName", (auth.baseInfo.authInfo == null) ? "" : escape(auth.baseInfo.authInfo.userName).toLocaleLowerCase().replace(/%u/gi, '\\u'));
                            }
                        },
                        select: function (event, object) {
                            var obj = { "clusterName": Number(object.item.id) };
                            return true;
                        },
                        source: "/api/record/signal/libraryList",
                        method: "post",
                        autoFilter: false,
                        autoFocus: false,
                        multiple: false,
                        parseValue: function () {
                            var val = this.element.val(),
                                ret = []
                            return ret;
                        },
                        getParam: function (term) {
                            var param = {"paramsObj": JSON.stringify({'EQL_recordSignal.status':"1",'LIKES_recordSignal.signal_name': $.trim(term)})};
                            return param;
                        },
                        process: function (json) {
                            var ret = [],
                                self = this,
                                options = self.options,
                                itemData = null;
                            var data = null;
                            if (json == null) {
                                return [];
                            }
                            if (json.code === "A00000") {
                                data = json.data == null ? null : json.data;
                            }
                            if (!data) {
                                return [];
                            }

                            $.map(data.result, function (item) {
                                itemData = {};
                                itemData[options.valueKey] = item["id"];
                                itemData[options.labelKey] = item["signalName"]
                                ret.push(itemData);
                            });
                            return ret;
                        },
                        template: function (item) {
                            return item.label;
                        }
                    }
                }
                $("input[name=signal_name]").suggest(signalSuggest.options);
            }
        });

    }
);